package com.zlt.controller.recruiter;


import com.zlt.controller.BaseController;
import com.zlt.dto.CompanyDTO;
import com.zlt.service.CompanyService;
import com.zlt.service.impl.CompanyServiceImpl;
import com.zlt.util.ClassUtil;
import com.zlt.util.StringUtil;
import com.zlt.vo.CompanyVO;
import com.zlt.vo.Pager;
import com.zlt.vo.Result;
import com.zlt.vo.UserVo;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "CompanyController", value = "/company/*")
public class CompanyController extends BaseController {

    private CompanyService companyService = new CompanyServiceImpl();

    @Override
    protected void defaultMethod(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }

    /**
     * 自己企业所有的hr
     *
     * @param request
     * @param response
     * @return
     * @throws ServletException
     * @throws IOException
     */
    protected Result allHr(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        try {
            String page = request.getParameter("page");
            if (!StringUtil.isNotNull(page)) {
                page = "1";
            }
            String limit = request.getParameter("limit");
            if (!StringUtil.isNotNull(limit)) {
                limit = "10";
            }


            Pager<UserVo> pager = new Pager<>();
            pager.setPage(Integer.parseInt(page));
            pager.setLimit(Long.parseLong(limit));
            pager = companyService.selectUserHr(pager);
            return Result.ok("查询成功", pager);

        } catch (RuntimeException e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }



    }

    /**
     * 待审核的hr
     *
     * @param request
     * @param response
     * @return
     * @throws ServletException
     * @throws IOException
     */
    protected Result reviewedHr(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


        //分页
        String page = request.getParameter("page");
        if (!StringUtil.isNotNull(page)) {
            page = "1";
        }
        String limit = request.getParameter("limit");
        if (!StringUtil.isNotNull(limit)) {
            limit = "10";
        }

        Pager<UserVo> pager = new Pager<>();
        pager.setPage(Integer.parseInt(page));
        pager.setLimit(Long.parseLong(limit));

        pager = companyService.selectReviewedHr(pager);
        return Result.ok("查询成功", pager);
    }

    /**
     * 通过hr的申请
     *
     * @param request
     * @param response
     * @return
     * @throws ServletException
     * @throws IOException
     */
    protected Result passHr(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String uid = request.getParameter("uid");

        System.out.println("passuidhr:" + uid);
        //校验
        try {
            boolean success = companyService.updateUserCompanyState(uid, 0);
            return success ? Result.ok("通过成功") : Result.error("通过失败");
        } catch (RuntimeException e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }

    /**
     * 拒绝hr的申请
     *
     * @param request
     * @param response
     * @return
     * @throws ServletException
     * @throws IOException
     */
    protected Result rejectHr(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String uid = request.getParameter("uid");

        System.out.println("rejuidhr:" + uid);

        //校验
        try {
            boolean success = companyService.updateUserCompanyState(uid, 2);
            return success ? Result.ok("拒绝成功") : Result.error("拒绝失败");
        } catch (RuntimeException e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }



    /**
     * 创建公司
     *
     * @param req
     * @param resp
     * @return
     * @throws ServletException
     * @throws IOException
     */
    protected Result createCompany(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        CompanyDTO companyDTO = ClassUtil.resolveRequest(CompanyDTO.class, req);
        System.out.println("regCompanyDto"+companyDTO);
        try {
            boolean success = companyService.createCompany(companyDTO);
            return success ? Result.ok("创建成功") : Result.error("创建失败");
        } catch (RuntimeException e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }

    /**
     * 查看当前审核状态
     * @param req
     * @param resp
     * @return
     * @throws ServletException
     * @throws IOException
     */
    protected Result all(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        try {
            String page = req.getParameter("page");
            if (!StringUtil.isNotNull(page)) {
                page = "1";
            }
            String limit = req.getParameter("limit");
            if (!StringUtil.isNotNull(limit)) {
                limit = "10";
            }


            Pager<CompanyVO> pager = new Pager<>();
            pager.setPage(Integer.parseInt(page));
            pager.setLimit(Long.parseLong(limit));
            pager = companyService.selectCompany(pager);
            return Result.ok("查询成功", pager);

        } catch (RuntimeException e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }
}
